package com.gitee.fsmxm.thinking.in.spring.bean.definition;

import com.gitee.fsmxm.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * bean定义构建demo
 */
public class BeanDefinitionCreationDemo {

    public static void main(String[] args) {

        // 1 通过 BeanDefinitionBuilder 构建
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);

        // 添加参数
        beanDefinitionBuilder.addPropertyValue("id", 1L)
                .addPropertyValue("name", "小马哥");

        // 获取 BeanDefinition 实例 (BeanDefinition 并非终态，可以进行修改)
        BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();

        // 2 通过 AbstractBeanDefinition 及其 派生类 来构建
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(User.class);

        MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
        mutablePropertyValues.add("id", 1L)
                .add("name", "小马哥");

        genericBeanDefinition.setPropertyValues(mutablePropertyValues);

    }

}
